﻿Imports System.Text
Imports QBuilder

<TestClass()>
Public Class CriteriaGroupTest

    <TestMethod()>
    Public Sub CriteriaGroup_And()
        Dim target As New CriteriaGroup()
        target.AddCriteria(New Criteria("A", Operation.EqualTo, "1"))
        target.AddCriteria(New Criteria("B", Operation.EqualTo, "2"))
        target.AddCriteria(New Criteria("C", Operation.EqualTo, "3"))

        Dim expected As String = "(A = 1 AND B = 2 AND C = 3)"
        Dim actual As String
        actual = target.GetText
        Assert.AreEqual(expected, actual)
    End Sub

    <TestMethod()>
    Public Sub CriteriaGroup_Or()
        Dim target As New CriteriaGroup()
        target.AddCriteria(New Criteria("A", Operation.EqualTo, "1"), BooleanOperator.Or)
        target.AddCriteria(New Criteria("B", Operation.EqualTo, "2"), BooleanOperator.Or)
        target.AddCriteria(New Criteria("C", Operation.EqualTo, "3"), BooleanOperator.Or)

        Dim expected As String = "(A = 1 OR B = 2 OR C = 3)"
        Dim actual As String
        actual = target.GetText
        Assert.AreEqual(expected, actual)
    End Sub

    <TestMethod()>
    Public Sub CriteriaGroup_AndOr()
        Dim target As New CriteriaGroup()
        target.AddCriteria(New Criteria("A", Operation.EqualTo, "1"))
        target.AddCriteria(New Criteria("B", Operation.EqualTo, "2"), BooleanOperator.And)
        target.AddCriteria(New Criteria("C", Operation.EqualTo, "3"), BooleanOperator.Or)

        Dim expected As String = "(A = 1 AND B = 2 OR C = 3)"
        Dim actual As String
        actual = target.GetText
        Assert.AreEqual(expected, actual)
    End Sub

    <TestMethod()>
    Public Sub CriteriaGroup_ChangeDefaultOp()
        Dim target As New CriteriaGroup()
        target.DefaultOperator = BooleanOperator.Or
        target.AddCriteria(New Criteria("A", Operation.EqualTo, "1"))
        target.AddCriteria(New Criteria("B", Operation.EqualTo, "2"))
        target.AddCriteria(New Criteria("C", Operation.EqualTo, "3"))

        Dim expected As String = "(A = 1 OR B = 2 OR C = 3)"
        Dim actual As String
        actual = target.GetText
        Assert.AreEqual(expected, actual)
    End Sub

    <TestMethod()>
    Public Sub CriteriaGroup_Single()
        Dim target As New CriteriaGroup()
        target.DefaultOperator = BooleanOperator.Or
        target.AddCriteria(New Criteria("A", Operation.EqualTo, "1"))

        Dim expected As String = "(A = 1)"
        Dim actual As String
        actual = target.GetText
        Assert.AreEqual(expected, actual)
    End Sub

    ''' <summary>
    ''' Makes sure that empty criteria groups render as empty strings
    ''' </summary>
    ''' <remarks></remarks>
    <TestMethod()>
    Public Sub CriteriaGroup_Empty()
        Dim target As New CriteriaGroup()
        target.DefaultOperator = BooleanOperator.Or

        Dim expected As String = ""
        Dim actual As String
        actual = target.GetText
        Assert.AreEqual(expected, actual)
    End Sub

    ''' <summary>
    ''' Tests to make sure that when a CriteriaGroup is the only criteria in it's parent it does not render parens around it's self.
    ''' </summary>
    ''' <remarks></remarks>
    <TestMethod()>
    Public Sub CriteriaGroup_NoParensOnOnlyChild()
        Dim target As New SelectQuery()
        target.Fields.Add("A")
        target.TableName = "SomeTable"

        Dim Group As New CriteriaGroup
        Group.AddCriteria(New Criteria("A", Operation.EqualTo, "1"))

        target.AddCriteria(Group)

        Dim expected As String = "SELECT A FROM SomeTable WHERE A = 1"
        Dim actual As String
        actual = target.GetSQLText
        Assert.AreEqual(expected, actual)
    End Sub

    ''' <summary>
    ''' Tests to make sure that when a CriteriaGroup is not the only child of it's parent that it properly renders parens around it's self.
    ''' </summary>
    ''' <remarks></remarks>
    <TestMethod()>
    Public Sub CriteriaGroup_ParensOnMultipleSiblings()
        Dim target As New SelectQuery()
        target.Fields.Add("A")
        target.TableName = "SomeTable"

        Dim Group1 As New CriteriaGroup
        Group1.AddCriteria(New Criteria("A", Operation.EqualTo, "1"))

        Dim Group2 As New CriteriaGroup
        Group2.AddCriteria(New Criteria("B", Operation.EqualTo, "2"))

        target.AddCriteria(Group1)
        target.AddCriteria(Group2)

        Dim expected As String = "SELECT A FROM SomeTable WHERE (A = 1) AND (B = 2)"
        Dim actual As String
        actual = target.GetSQLText
        Assert.AreEqual(expected, actual)
    End Sub
End Class